/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-25 17:42:54
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-03-05 15:58:08
 * @FilePath: /yt-devops/lib/configs.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2023-04-01 17:51:03
 * Last Modified: 2023-04-23 09:39:16
 * Description:
 */

import 'dart:ui';

import 'package:flutter/foundation.dart';
// import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:native_dart_package/http/env/env_model.dart';
import 'package:native_dart_package/native_dart_package.dart' as client;
import 'package:native_dart_package/store/sp_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'components/log_utils.dart';

enum Environment { debug, profile, release }

class Configs {
  Configs._internal();

  factory Configs() => _instance;

  static final Configs _instance = Configs._internal();

  /// 请求 client，初始化分三种场景
  /// 1. App 启动的时候，初始化一次
  /// 2. 设置代理后，需要初始化一次
  /// 3. 手动选择服务器环境的时候，需要初始化一次
  static late client.HTTPClient httpClient;

  static late EnviromentModel enviromentModel;

  Future<EnviromentModel> get environment async {
    var prefs = await SharedPreferences.getInstance();
    String apiURL =
        prefs.getString('currentEnvironment') ?? 'https://ytoss.yotaienergy.com';
    String webURL = 'http://ytoss.tellhowdl.com:8989/PrivacyPolicy.html';
    String privateUrl = 'http://ytoss.tellhowdl.com:8989/PrivacyPolicy.html';

    String env = "prod";
    if (kDebugMode) {
      env = "dev";
    } else if (kProfileMode) {
      env = "test";
    } else {
      env = "prod";
    }
    return EnviromentModel(
        api: apiURL, env: env, webUrl: webURL, privacyPolicy: privateUrl);
  }

  Future<void> loadConfigs() async {
    try {
      //先获取缓存里的是否有选择过数据中心
      // var local = SpSotre.instance.getStringMap(EnviromentModel.environmentKey);
      var prefs = await SharedPreferences.getInstance();
      Log.d('prefs.getStrin==${prefs.getString('currentEnvironment')}');
      var environmentString = prefs.getString('currentEnvironment');
      if (environmentString != null && environmentString.isNotEmpty) {
        //是，获取并更新到全局
        EnviromentModel environmentModel = EnviromentModel(
            api: environmentString,
            privacyPolicy: 'http://ytoss.tellhowdl.com:8989/PrivacyPolicy.html',
            env: 'prod');
        enviromentModel = environmentModel;
      } else {
        // 否，则加载配置文件里的主数据中心
        print("加载配置文件里的主数据中心");
        // if (kDebugMode) {
        //   await dotenv.load(fileName: 'configs/.env.debug');
        // } else if (kProfileMode) {
        //   await dotenv.load(fileName: 'configs/.env.profile');
        // } else {
        //   await dotenv.load(fileName: 'configs/.env.release');
        // }
        enviromentModel = await Configs().environment;
      }
      httpClient = client.HTTPClient(environment: enviromentModel);
    } catch (error) {
      print("load file with error: ${error.toString()} ");
    }
  }

  setLocale(Locale locale) {
    enviromentModel.locale = locale;
    httpClient = client.HTTPClient(environment: enviromentModel);
    Log.d('locale==============2222=' + locale.toString());
  }
}
